<?php
class customstock_model extends CI_Model{
	private $kode_barang;
	private $qty;
	
	function setKodeBarang($kBarang){
		$this->kode_barang = $kBarang;
	}
	
	function getKodeBarang(){
		return $this->kode_barang;
	}
	
	function setQty($qt){
		$this->qty = $qt;
	}
	
	function getQty(){
		return $this->qty;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}

	function getItemCategory(){
		$this->db->select("ph.kodebarang,ph.productname itemcategory")->where(array('active'=>'1'));
		$query = $this->db->get("product_header ph");
		return $query->result();
	}

	/*select dp.pruductname itemname,ph.pruductname itemcategory,sum(stockawal)stockawal,sum(stockakhir)stockakhir from detail_product dp
	 left join stock st on dp.kodedetailbarang = st.kodedetailbarang
	 left join product_header ph on dp.kodebarang=ph.kodebarang
	 group by dp.pruductname,ph.pruductname */	
	function getStockList(){
		$this->db->select('dp.kodedetailbarang,ph.kodebarang,dp.itemprice,dp.productname itemname,ph.productname itemcategory,dp.typeunit,sum(stockakhir) stokakhir')
		->join('stock st','dp.kodedetailbarang = st.kodedetailbarang','left')
		->join('product_header ph','dp.kodebarang = ph.kodebarang','left')
		->where(array('dp.KodeBarang' => $this->getKodeBarang()))
		->group_by(array('dp.productname','ph.productname','dp.kodedetailbarang','ph.kodebarang'));
		$query = $this->db->get('detail_product dp');
		return $query->result();
	}
	
	function getFakturAutoNo(){
		
		$query = $this->db->query("select MAX(RIGHT(NoFaktur,4)) as kdfak from faktur_product_penjualan");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idfak)
			{
				$tmpfak = ((int)$idfak->kdfak)+1;
				$kd = sprintf("%04s", $tmpfak);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "FK".$kd; 
		
		return $kd;
		
		/*
		$this->db->select('nofaktur');
		$query = $this->db->get('faktur_product_penjualan');
		return $query->result();*/
	}

	function getItemsContent(){
		$this->db->select('dp.kodedetailbarang,dp.productname,dp.typeunit,sup.kodesupplier,sup.suppliername')
		->join('detail_product dp','dp.kodedetailbarang = st.kodedetailbarang','left')
		->join('supplier sup','sup.kodesupplier = st.kodesupplier')
		->where(array('dp.kodedetailbarang' => $this->getKodeBarang()));
		$query = $this->db->get('stock st');		
		return $query->result();
	}
}